package com.mySampleApplication.client.History;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterService;
import com.mySampleApplication.client.codeRule.CodeRuleService;
import com.mySampleApplication.client.codeRule.CodeRuleWindow;
import com.mySampleApplication.client.codeRule.SbWindow;
import com.mySampleApplication.client.components.SDialog;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedEvent;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedHandler;
import com.smartgwt.client.widgets.layout.HLayout;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class CodeRule {
    private static ListGrid plistGrid;


    public static ListGrid getPlistGrid() {
        return plistGrid;
    }

    public static void setPlistGrid(ListGrid plistGrid) {
        CodeRule.plistGrid = plistGrid;
    }

    public static Map<String,Object> getListGrid() {
        Map<String,Object> map =new HashMap<>();
        // 创建一个列表网格控件
        ListGrid listGrid =getOp();

        ListGrid finalListGrid = listGrid;
        listGrid.setWidth100(); // 设置宽度为100%
        listGrid.setHeight100(); // 设置高度为100%
//        listGrid.setWidth(1024);
//        listGrid.setHeight(224);
        // 设置列表网格显示网格线
        listGrid.setShowAllRecords(false); // 显示所有记录
        // 开启分页功能
        listGrid.setDataPageSize(10); // 每页显示的数据条数
        //listGrid.setShowFilterEditor(true); // 显示过滤编辑器
        //listGrid.setSelectionType(SelectionStyle.MULTIPLE);//开启多选框
        //listGrid.setShowRowNumbers(true); // 显示行号
        //listGrid.setPaginationMode(PaginationMode.LOCAL); // 设置分页模式为本地分页
        //允许用户编辑行内容
        //listGrid.setCanEdit(true);
        //listGrid.setEditEvent(ListGridEditEvent.DOUBLECLICK); // 双击进行编辑
        listGrid.setModalEditing(true);
        listGrid.setVirtualScrolling(false);
        listGrid.setShowRecordComponents(true);
        listGrid.setShowRecordComponentsByCell(true);
        //listGrid.setCanRemoveRecords(true);


//        ListGridField selectedCountriesField = new ListGridField("name", "Selected");
//        selectedCountriesField.setWidth(70);
//        listGrid.setFields(selectedCountriesField);
//        listGrid.setSelectionType(SelectionStyle.SIMPLE);
//        listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

        // 定义列表网格的列
        ListGridField idField = new ListGridField("id", "序号");
        idField.setCellFormatter(new CodeRule.CustomCellFormatter());
        idField.setWidth(50);
        //ListGridField db_model_idField = new ListGridField("db_model_id", "数据模型");

        ListGridField db_model_mc = new ListGridField("db_model_mc", "数据模型名称");

        ListGridField mcField = new ListGridField("mc", "规则名称");
        //mcField.setCellFormatter(new CustomCellFdmcFormatter());
        ListGridField msField = new ListGridField("ms", "描述");

        ListGridField fgfField = new ListGridField("fgf", "分隔符");

        ListGridField qzField = new ListGridField("qz", "前缀");

        ListGridField hzField = new ListGridField("hz", "后缀");

        ListGridField statusField = new ListGridField("status", "状态");
        ListGridField bbField = new ListGridField("bb", "版本号");
        bbField.setCellFormatter(new CustomCellbbFormatter());
        ListGridField actionField = new ListGridField("actionField", "操作");
        //actionField.setWidth(300);
        listGrid.setFields(idField, db_model_mc,mcField,msField,fgfField, qzField,hzField,statusField,bbField,actionField);

        // 添加 CellClickHandler 处理单击事件
        listGrid.addCellClickHandler(new CellClickHandler() {
            @Override
            public void onCellClick(CellClickEvent event) {
                // 获取单击发生的行和列
                int rowNum = event.getRowNum();
                int colNum = event.getColNum();

                // 判断是否单击了指定的列
                if (colNum == listGrid.getFieldNum("db_model_mc")) {
                    // 获取单击处的数据
                    ListGridRecord record = listGrid.getRecord(rowNum);
//                    String value = record.getAttribute("fdmc");
//                    SC.say( "弹窗显示详情！"+record.getAttribute("fdmc"));
                    details(record,listGrid,true);
                    // 执行其他操作...
                }else if(colNum == listGrid.getFieldNum("bb")){
//                    ListGridRecord record = listGrid.getRecord(rowNum);
//                    HistoryWindow.getShowWindow(700,500,"历史版本",record).show();
                }
            }
        });

        setPlistGrid(listGrid);
        map.put("listGrid",listGrid);
        //map.put("buttonLayout",buttonLayout);
        return map;

    }

    private static ListGrid getOp(){
        return new ListGrid() {
            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {

                String fieldName = this.getFieldName(colNum);

                if (fieldName.equals("actionField")) {
                    HLayout recordCanvas = new HLayout(3);
                    recordCanvas.setHeight(22);
                    recordCanvas.setWidth100();
                    recordCanvas.setAlign(Alignment.CENTER);

                    IButton hqbmImg = new IButton();
                    hqbmImg.setTitle("详情");
                    hqbmImg.setWidth(60);
//                    editImg.setRight(10);
                    hqbmImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            details(record,getPlistGrid(),true);

                        }
                    });
                    recordCanvas.addMember(hqbmImg);


                    IButton fdImg = new IButton();
                    fdImg.setTitle("分段配置");
                    //fdImg.setIcon("[SKIN]/actions/edit.png"); // 设置图标路径
//                    editImg.setShowDown(false);
//                    editImg.setShowRollOver(false);
//                    editImg.setLayoutAlign(Alignment.CENTER);
//                    editImg.setSrc("[SKIN]/actions/edit.png");
//                    editImg.setPrompt("编辑");
//                    editImg.setHeight(16);
                    fdImg.setWidth(65);
//                    editImg.setRight(10);
                    fdImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            createSidePanel(record,event);
                            //SC.say("Edit Comment Icon Clicked for country : " + record.getAttribute("id"));
                        }
                    });


                   // recordCanvas.addMember(fdImg);

                    return recordCanvas;
                } else {
                    return null;
                }

            }
        };
    }

    private static void showqDialog(ListGrid listGrid) {
        Dialog dialog = SDialog.createDialog(1400,650,"表单组件", null,listGrid);
        dialog.show();
    }


    public static void showAddDialog(ListGrid listGrid) {
//        Dialog dialog = SDialog.createDialog("Add Item", null,listGrid);
        Window dialog= CodeRuleWindow.getShowWindow(0,1000,350,"新增编码规则", null,listGrid,false,"","","","",null);
        dialog.show();
    }



    private static void showEditDialog(ListGrid listGrid) {
        ListGridRecord selectedRecord = listGrid.getSelectedRecord();
        if (selectedRecord != null) {
            //Dialog dialog = SDialog.createDialog("Edit Item", selectedRecord,listGrid);
            Window dialog=CodeRuleWindow.getShowWindow(0,1000,350,"编辑编码规则", selectedRecord,listGrid,false,"","","","",null);
            dialog.show();
        } else {
            SC.say("Please select an item to edit.");
        }
    }

    // 编辑记录函数
    private static void editRecord(ListGridRecord record, ListGrid listGrid,boolean istrue) {
        Window dialog=CodeRuleWindow.getShowWindow(0,1000,350,"编辑编码规则", record,listGrid,istrue,"","","","",null);
        dialog.show();
        // 执行编辑操作
    }

    // 详情
    private static void details(ListGridRecord record, ListGrid listGrid,boolean istrue) {
        Window dialog=CodeRuleWindow.getShowWindow(0,1000,650,"编码规则详情", record,listGrid,istrue,"","","","",null);
        dialog.show();
        // 执行编辑操作
    }

    // 升版记录函数
    private static void sbRecord(ListGridRecord record, ClickEvent event,boolean istrue) {
        Window dialog= SbWindow.getShowWindow(1,1000,350,"编码规则升版", record,getPlistGrid(), istrue);
        dialog.show();
        // 执行编辑操作
    }

    // 删除记录函数
    private static void deleteRecord(ListGridRecord record, ClickEvent event) {
        ListGrid listGrid = getPlistGrid();
        listGrid.removeData(record);
        if(record.getAttribute("status").equals("禁用")){
            // 执行删除操作
            CodeRuleAtterService.App.getInstance().deleteByCoderuleId(record.getAttribute("id"),new AsyncCallback<String>() {
                public void onSuccess(String result) {
//                    SC.say( "删除成功！");
                    CodeRuleService.App.getInstance().deleteById(record.getAttribute("id"),new AsyncCallback<String>() {
                        public void onSuccess(String result) {
                            SC.say( "删除成功！");
                        }
                        public void onFailure(Throwable caught) {
                            SC.warn("RPC failed");
                        }
                    });
                }
                public void onFailure(Throwable caught) {
                    SC.warn("RPC failed");
                }
            });

        }else{
            SC.say( "状态为启用的编码规则不可进行删除！");
        }

    }


    public static void createSidePanel(ListGridRecord record, ClickEvent event) {
        Window dialog= FdpzWindow.getShowWindow(1200,700,"分段配置",record);
        dialog.show();
        // 执行分段配置操作
    }
    // 修改状态
    public static void updateStatus(ListGridRecord record, ClickEvent event) {
        Map<String, String> map = new HashMap<>();
        map.put("id", record.getAttribute("id"));
        if(record.getAttribute("status").equals("禁用")){
            map.put("status","启用");
        }else{
            map.put("status","禁用");
        }
        CodeRuleService.App.getInstance().setStatus(map,new AsyncCallback<String>() {
            public void onSuccess(String result) {
//                SC.say( "删除成功！");
            }
            public void onFailure(Throwable caught) {
                SC.warn("RPC failed");
            }
        });
    }

    // 自定义的 Formatter
    private static class CustomCellFdmcFormatter implements com.smartgwt.client.widgets.grid.CellFormatter {
        @Override
        public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            return "<span style='color: blue;cursor: pointer;'>" + value + "</span>"; // 使用 HTML 标签设置字体样式
        }
    }
    // 自定义的 Formatter
    private static class CustomCellbbFormatter implements com.smartgwt.client.widgets.grid.CellFormatter {
        @Override
        public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            return "<span style='color: #fff;\n" +
                    "    background-color: #5de55da3;\n" +
                    "    \n" +
                    "    border-radius: 3px;\n" +
                    "    /* border: 1px silver solid; */\n" +
                    "    width: 30px;\n" +
                    "    padding: 0 20px;'>" + value + "</span>"; // 使用 HTML 标签设置字体样式
        }
    }
    // 自定义的 Formatter
    private static class CustomCellFormatter implements com.smartgwt.client.widgets.grid.CellFormatter {
        @Override
        public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            return Integer.toString(rowNum + 1); // 返回当前行的序号（行号）
        }
    }
}
